#include "CsvReader.h"

#include <fstream>
using std::ifstream;
using std::getline;

#include <sstream>
using std::stringstream;

vector<Client> CsvReader::readFile( const string& fileName)
{
	vector<Client> data;

	ifstream file(fileName);

	string line;
	getline(file,line); // ignore the first line wich is cvs data format
	getline(file,line);
	while (!file.eof())
	{
		data.push_back( readLine(line) );
		getline(file,line);
	}

	return data;
}

Client CsvReader::readLine(const string & line)
{
	stringstream lineStream(line);
	
	string strId;
	getline(lineStream,strId,',');

	string strAge;
	getline(lineStream,strAge,',');
	short age;
	stringstream(strAge) >> age;

	string strSex;
	getline(lineStream,strSex,',');
	Sex sex = (strSex == "FEMALE") ? female : male;

	string strRegion;
	getline(lineStream,strRegion,',');
	Region region = (strRegion == "INNER_CITY") ? inner_city : 
					(strRegion == "TOWN") ? town :
					(strRegion == "RURAL") ? rural : suburban;
	
	string strIncome;
	getline(lineStream,strIncome,',');
	double income;
	stringstream(strIncome) >> income;

	string strIsMarried;
	getline(lineStream,strIsMarried,',');
	bool isMarried = (strIsMarried == "YES");

	string strChildren;
	getline(lineStream,strChildren,',');
	short children;
	stringstream(strChildren) >> children;

	string strHasCar;
	getline(lineStream,strHasCar,',');
	bool hasCar = (strHasCar == "YES");

	string strHasSavingAccount;
	getline(lineStream,strHasSavingAccount,',');
	bool hasSavingAccount = (strHasSavingAccount == "YES");

	string strHasCurrentAccount;
	getline(lineStream,strHasCurrentAccount,',');
	bool hasCurrentAccount = (strHasCurrentAccount == "YES");

	string strHasMortage;
	getline(lineStream,strHasMortage,',');
	bool hasMortage = (strHasMortage == "YES");

	string strHasPEP;
	getline(lineStream,strHasPEP,',');
	bool hasPEP = (strHasPEP == "YES");

	return Client(strId,age,sex,region,income,isMarried,children,hasCar,hasSavingAccount,hasCurrentAccount,hasMortage,hasPEP);
}